// pages/cart/cart.js
var md5Js = require('../../utils/md5.js');
var utils = require('../../utils/util.js');
var cart  = require('../../utils/cart.js');
var app = getApp();//获取全局注册函数
var object = {};
object.data = {
  motto: '',
  cart_num: [],
  imgUrls: [],
  function_list: [],
  product_list: [],
  indicatorDots: true,
  autoplay: true,
  interval: 5000,
  duration: 1000,
  actionSheetHidden: true,
  activeIndex: 0,
  sku_info_checkout: [],
  num: 1,
  index: 0,
  inputShowed: false,
  inputVal: ""
};
object.onLoad = function (options) {
  try {
    var that = this;
    //获取缓存的用户对象信息
    var user_info = wx.getStorageSync('user_info');
    console.log(user_info);
    //如果用户信息不为空则请求首页接口获取产品等信息。
    if (user_info) {
      var request_data = user_info.user_itmes;
      var uid = request_data.id.toString();
      var private_key = user_info.private_key;
      var t = new Date().getTime().toString();
      var k = md5Js.hex_md5(private_key + t);//key
      var parms_url = app.request_url + 'C_Home/?';
      parms_url += 'enterpriseId=' + request_data.enterpriseId;
      parms_url += '&channelId=' + request_data.channelId;
      parms_url += '&uid=' + request_data.id;
      parms_url += '&userType=' + request_data.type;
      parms_url += '&t=' + t;
      parms_url += '&k=' + k;
      parms_url += '&v=8';
      parms_url += '&l=999';
      // parms_url = 'https://rest.test.freshqiao.com/C_Home/?enterpriseId=79&channelId=204&uid=100000202&userType=1&t=1484738433392&k=c9b791f7477e04ed5754b9144d304f22&v=2&l=999';
      //https://rest.test.freshqiao.com/C_Home/?enterpriseId=79&channelId=200&userType=1&t=1496643718578&k=591a1c64d90a7a3bacc4e5e13b8018c5&uid=100000594&v=8
      //调用首页接口接口
      wx.request({
        url: parms_url,
        method: 'GET',
        success: function (res) {
          var respones = res;
          //请求成功分配模板变量
          if ((respones.statusCode == '200') && (respones.data.Success == 1)) {
            var data = respones.data.CallInfo;
            data.middle_function_area.shift();
            var midddle_function_area_length = data.middle_function_area.length;
            //截取middle_function_area中的title
            for(var i = 0; i< midddle_function_area_length; i++){
              var title = data.middle_function_area[i].title.substring(0,4);
              data.middle_function_area[i].title_string = title;
            }
            //企业热卖
            var data_product = data.product_list;
            var product_length = data_product.length;
            for (var i = 0; i < product_length; i++) {
              var price = data_product[i].sku_info[0].sellprice;
              var scell_price = 0
              if (price) {
                scell_price = price;
              }
              data_product[i].sku_info_string = JSON.stringify(data_product[i].sku_info);
              data_product[i].scell_price_string = scell_price;
            }
            //新品上架
            var news_product_list = data.news_product_list;
            var news_product_list_length = news_product_list.length;
            for (var i = 0; i < news_product_list_length; i++) {
              var price = news_product_list[i].sku_info[0].sellprice;
              var scell_price = 0
              if (price) {
                scell_price = price;
              }
              news_product_list[i].sku_info_string = JSON.stringify(data_product[i].sku_info);
              news_product_list[i].scell_price_string = scell_price;
            }
            //促销商品
            var market_product_list = data.market_product_list;
            var market_product_list_length = market_product_list.length;
            for (var i = 0; i < market_product_list_length; i++) {
              var price = market_product_list[i].sku_info[0].sellprice;
              var scell_price = 0
              if (price) {
                scell_price = price;
              }
              market_product_list[i].sku_info_string = JSON.stringify(data_product[i].sku_info);
              market_product_list[i].scell_price_string = scell_price;
            }
            var cart_info = wx.getStorageSync('cart_info');
            wx.setStorageSync('product_info', data_product);
            //分配模板变量
            that.setData({
              imgUrls: data.advertise_list,
              function_list: data.middle_function_area,
              product_list: data_product,
              news_product_list: news_product_list,
              market_product_list: market_product_list,
              cart_num: cart_info.length
            });
          }
        }
      })
    }
  } catch (e) {
    console.log('error=>' + e.message); return false;
  }
};
//搜索
object.showInput = function () {
  wx.navigateTo({
    url: '../search/search',
  })
},

  //跳转到企业风采、新品上架、我的收藏、最近购买、所有商品
  object.jump = function (e) {
    var title = e.currentTarget.dataset.title;
    if (title == '企业风采') {
      wx.navigateTo({
        url: '../enterpriseStyle/enterpriseStyle',
      })
    } else if (title == '新品上架') {
      wx.navigateTo({
        url: '../newProduct/newProduct',
      })
    } else if (title == '我的收藏') {
      wx.navigateTo({
        url: '../myCollection/myCollection',
      })
    } else if (title == '最近购买') {
      wx.navigateTo({
        url: '../history/history',
      })
    } else {
      wx.switchTab({
        url: '../shop/shop',
      })
    }
  },
  //加载更多
  object.loadmore = function() {
    wx.switchTab({
      url: '../shop/shop',
    })
  },
object.hideModal = function () {
  // 隐藏遮罩层
  var animation = wx.createAnimation({
    duration: 200,
    timingFunction: "linear",
    delay: 0
  })
  this.animation = animation
  animation.translateY(300).step()
  this.setData({
    animationData: animation.export(),
  })
  setTimeout(function () {
    animation.translateY(0).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: false
    })
  }.bind(this), 200)
}

  //点击产品时进入产品详情页
  object.productdetail = function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../shopdetail/shopdetail?id=' + id,
    })
  },
  object.onReady = function () {
    //this.showModal();
    //页面加载完成调用的方法
  };
object.onShow = function () {
  //从后台进入前台显示页面调用
};
object.onHide = function () {
  //从前台进入后台隐藏页面调用
};
object.onUnload = function () {
  //页面关调用
};

//点击购物车图片时添加商品
object.standardItem  = cart.standardItem;// 购物车规格点击事件
object.spec = cart.spec; //购物车点击属性
object.submit =cart.submitcart;
object.showModal =cart.showModal;
object.bindMinus = cart.bindMinus;
object.bindPlus = cart.bindPlus;
//分配模板变量
Page(object);

